package com.example.market2023.service;

import com.example.market2023.dao.SuppliersDao;
import com.example.market2023.entity.Suppliers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SuppliersService implements SuppliersDao {
    //实现操作方法
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int add(Suppliers suppliers) {
        String sql="insert into suppliers values(null,?,?,?,'0')";
        Object[] args= {suppliers.getName(),suppliers.getAddr(),suppliers.getPhone()};
//        System.out.println(sql);
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(Suppliers suppliers) {
        String sql = "delete from suppliers where id=?";
        Object[] args={suppliers.getId()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int update(Suppliers suppliers) {
        String sql = "update suppliers set name=?,addr=?,phone=? where id=?";
        Object[] args={
                suppliers.getName(),suppliers.getAddr(),suppliers.getPhone(),suppliers.getId()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Suppliers findOne(Suppliers suppliers) {
        String sql="select * from suppliers where id="+suppliers.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Suppliers>(Suppliers.class));
    }

    @Override
    public List<Suppliers> findAll() {
        String sql = "select * from suppliers";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Suppliers>(Suppliers.class));
    }
}
